
#include "Array.h"


template<typename T>
Array<T>::Array()
{
    m_aNodeArray = new NODE<T>(m_sConf.m_nArray_Size);
    int _i = 0;
    while(_i < m_sConf.m_nArray_Size)
    {
        m_aNodeArray[_i].m_nIsUsed = 0;
        m_aNodeArray[_i].m_nNodeId = _i;
        _i++;
    }
}

template<typename T>
Array<T>::Array(struct Array_Conf a_sConf)
{
    this->m_sConf = a_sConf;
    m_aNodeArray = new NODE<T>(m_sConf.m_nArray_Size);
    int _i = 0;
    while(_i < m_sConf.m_nArray_Size)
    {
        m_aNodeArray[_i].m_nIsUsed = 0;
        m_aNodeArray[_i].m_nNodeId = _i;
        _i++;
    }
}

template<typename T>
int Array<T>::add(T* a_tData)
{
    m_aNodeArray[m_sConf.m_nPosition].m_nIsUsed = 1;
    m_aNodeArray[m_sConf.m_nPosition].m_tData = *a_tData;
    
    m_sConf.m_nPosition++;

    return 0;
}

template<typename T>
T* Array<T>::getByIndex(int a_nNum)
{
    if(a_nNum >= m_sConf.m_nArray_Size)
    {
        // 未触发错误，errno并没有被赋什么值
        fprintf(stderr, "Too large for the Array. \n");
        exit(1);
    }
    else if(a_nNum < 0)
    {
        fprintf(stderr, "Too small for the Array. \n");
        exit(1);
    }
    else
    {
        return &(m_aNodeArray[a_nNum].m_tData);
    }
}

template<typename T>
int Array<T>::size()
{
    int _nSize = 0;
    while(m_aNodeArray[_nSize].m_nIsUsed != 0)
    {
        _nSize++;
    }
    return _nSize;
}